package com.liy.framework.common.websocket.server;

import com.liy.framework.common.utils.JSONUtils;
import com.liy.framework.common.utils.Tools;
import com.liy.framework.common.webmsg.WebMessage;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WebMessageUtil {

    private static Logger log = LoggerFactory.getLogger(WebMessageUtil.class);
    public static String SUCCESS = "success";


    public static String sendToClient(String clientId, WebMessage wb) {
        try {
            if (Tools.StringIsNullOrSpace(clientId)) {
                return "消息目标不能为空";
            } else if (wb != null && !Tools.StringIsNullOrSpace(wb.getTitle()) && !Tools.StringIsNullOrSpace(wb.getContents())) {
                if ("03".equals(wb.getType())) {
                    WebSocketServer.sendToClient(clientId, JSONUtils.object2Json(wb), true);
                } else {
                    WebSocketServer.sendToClient(clientId, JSONUtils.object2Json(wb));
                }

                return SUCCESS;
            } else {
                return "消息标题、内容不能为空";
            }
        } catch (Exception var3) {
            log.error("发送客户端消息时发生错误：" + var3.getMessage(), var3);
            return "发送客户端消息时发生错误：" + var3.getMessage();
        }
    }

    public static String sendToAll(String clientId, WebMessage wb) {
        try {
            if (Tools.StringIsNullOrSpace(clientId)) {
                return "消息目标不能为空";
            } else if (wb != null && !Tools.StringIsNullOrSpace(wb.getTitle()) && !Tools.StringIsNullOrSpace(wb.getContents())) {
                WebSocketServer.sendToAll(JSONUtils.object2Json(wb));
                return SUCCESS;
            } else {
                return "消息标题、内容不能为空";
            }
        } catch (Exception var3) {
            log.error("发送消息时发生错误：" + var3.getMessage(), var3);
            return "发送消息时发生错误：" + var3.getMessage();
        }
    }

    public static String sendToGroup(String groupId, WebMessage wb) {
        try {
            if (Tools.StringIsNullOrSpace(groupId)) {
                return "消息目标不能为空";
            }

            if (wb == null || Tools.StringIsNullOrSpace(wb.getTitle()) || Tools.StringIsNullOrSpace(wb.getContents())) {
                return "消息标题、内容不能为空";
            }

            WebSocketServer.sendToGroup(groupId, JSONUtils.object2Json(wb));
        } catch (IOException var3) {
            log.error("发送分组消息时发生错误：" + var3.getMessage(), var3);
            return "发送分组消息时发生错误：" + var3.getMessage();
        }

        return SUCCESS;
    }

    public static String sendToRole(String clientId, WebMessage wb) {
        return SUCCESS;
    }
}
